package controllers

import (
	beego "github.com/beego/beego/v2/server/web"
	"house/models"
)

type HouseController struct {
	beego.Controller
}

type JsonR struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (c *HouseController) Add() {
	Title := c.GetString("title")
	Jj := c.GetString("jj")
	Images := c.GetString("images")
	Time := c.GetString("time")
	Man := c.GetString("man")

	allhouse := models.House{
		Title:  Title,
		Jj:     Jj,
		Images: Images,
		Time:   Time,
		Man:    Man,
	}

	err := models.DB.Create(&allhouse).Error
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "添加失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "添加成功",
			Data: nil,
		}
		c.ServeJSON()
	}

}
func (c *HouseController) List() {
	allhouse := []models.House{}
	err := models.DB.Find(&allhouse)
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "查询失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "查询成功",
			Data: allhouse,
		}
		c.ServeJSON()
	}
}
func (c *HouseController) Del() {
	id, _ := c.GetInt("id")
	err := models.DB.Delete(&models.House{}, id).Error
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "删除失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "删除成功",
			Data: nil,
		}
		c.ServeJSON()
	}
}
func (c *HouseController) Up() {
	id, _ := c.GetInt("id")
	Title := c.GetString("title")
	Jj := c.GetString("jj")
	Images := c.GetString("images")
	Time := c.GetString("time")
	Man := c.GetString("man")

	allhouse := models.House{
		Id:     id,
		Title:  Title,
		Jj:     Jj,
		Images: Images,
		Time:   Time,
		Man:    Man,
	}

	err := models.DB.Create(&allhouse).Error
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "修改失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "修改成功",
			Data: nil,
		}
		c.ServeJSON()
	}

}
